package jp.sourceforge.akjrcp.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.commons.util.FileNameUtils;



import org.eclipse.jface.preference.IPreferenceStore;

public class DefaultSetter {


	public static boolean setFilePath(IPreferenceStore store,String key,String[] values){
		boolean ret=false;
		for (int i = 0; i < values.length; i++) {
			if(new File(values[i]).exists()){
				store.setDefault(key,values[i]);
				return true;
			}
		}
		store.setDefault(key,"");
		return ret;
	}
	
	public static boolean setFilePaths(IPreferenceStore store,String key,String[] values){
		boolean ret=false;
		String paths="";
		for (int i = 0; i < values.length; i++) {
			File f=new File(values[i]);
			if(f.exists()&&f.isDirectory()){
				paths+=values[i]+";";
				//log.infoexist:"+values[i]);
			}
		}
		
		
		if(paths.length()>0){
		store.setDefault(key,paths.substring(0,paths.length()-1));//chop
		ret=true;
		}else{
		store.setDefault(key,"");
		}
		return ret;
	}
	
	//
	//some app version is too old
	//TODO release new version and fix it.
	public static  String[] toInstalledDirectory(String[] appNames,String others[]){
		String checkDir[]={"","akj_friends_2007_november\\","akj_friends_2007_july\\","akj_friends_2006_summer\\"};
		List<String> arrayList=new ArrayList<String>();
		for(int j=0;j<checkDir.length;j++){
		for (int i = 0; i < appNames.length; i++) {
			arrayList.add(System.getProperty("user.dir")+"\\"+checkDir[j]+appNames[i]);//application dir.
			arrayList.add(System.getProperty("user.dir")+"\\bin\\"+checkDir[j]+appNames[i]);//application dir.
			arrayList.add("c:\\Program Files\\"+checkDir[j]+appNames[i]);
		
			arrayList.add(FileNameUtils.getMyDocumentsPath()+"\\"+checkDir[j]+appNames[i]);
			arrayList.add(System.getProperty("user.home")+"\\"+Messages.getString("desktop")+"\\"+checkDir[j]+appNames[i]);
			}
		}
		//add other path
		for (int i = 0; i < others.length; i++) {
			arrayList.add(others[i]);
		}

		return (String[])arrayList.toArray(new String[0]);
	}
}
